//
//  ComplexMultiSelectViewController.m
//  DebugDemo
//
//  Created by XFB on 2019/6/12.
//  Copyright © 2019 ZhouCheng. All rights reserved.
//

#import "ComplexMultiSelectViewController.h"
#import "ProblemModel.h"

@interface ComplexMultiSelectViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSMutableArray *selectDataSource;

@end

@implementation ComplexMultiSelectViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setupBasicSet];
    [self setupUI];
}

- (void)setupBasicSet
{
    self.view.backgroundColor = UIColorFromRGB(0xFFFFFF);
    
    self.selectDataSource = @[].mutableCopy;
    
    NSMutableArray *dataArray = @[].mutableCopy;
    for (ProblemModel *problemM in self.problemArray) {
        problemM.isCellSelected = problemM.isDataSelected;
        if (problemM.isDataSelected) {
            [self.selectDataSource addObject:problemM];
        }
        [dataArray addObject:problemM];
    }
    self.dataSource = [dataArray mutableCopy];
}

- (void)setupUI
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavBarHeight, ScreenW, ScreenH-NavBarHeight-BottomHeight-70) style:UITableViewStylePlain];
    tableView.backgroundColor = UIColorFromRGB(0xFFFFFF);
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.tableFooterView = [UIView new];
    tableView.userInteractionEnabled = YES;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAlways;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self.tableView reloadData];
    
    UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [saveButton addTarget:self action:@selector(saveButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
    saveButton.frame = CGRectMake(15, ScreenH-60-BottomHeight, ScreenW-30, 50);
    [saveButton setTitle:@"保存" forState:UIControlStateNormal];
    saveButton.titleLabel.font = [UIFont systemFontOfSize:16];
    saveButton.backgroundColor = UIColorFromRGB(0x1abc9c);
    saveButton.layer.cornerRadius = 5;
    saveButton.layer.masksToBounds = YES;
    [self.view addSubview:saveButton];
}

- (void)saveButtonDidClick
{
    NSLog(@"%ld", self.selectDataSource.count);
    /// 将CellSelected赋值给DataSelected
    NSMutableArray *mutableArray = @[].mutableCopy;
    for (ProblemModel *problemM in self.problemArray) {
        problemM.isDataSelected = problemM.isCellSelected;
        [mutableArray addObject:problemM];
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark --- <UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    ProblemModel *problemM = self.dataSource[indexPath.row];
    cell.textLabel.text = problemM.name;
    if (problemM.isCellSelected) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    return cell;
}

#pragma mark --- <UITableViewDelegate>

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    ProblemModel *problemM = self.dataSource[indexPath.row];
    if (problemM.isCellSelected) {
        problemM.isCellSelected = NO;
        [self.selectDataSource removeObject:problemM];
    } else {
        problemM.isCellSelected = YES;
        [self.selectDataSource addObject:problemM];
    }
    [self.tableView reloadData];
}

@end

